﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RXY_Mall.Core
{
    /// <summary>
    /// 坐标转换类
    /// </summary>
    public class LocationChage
    {
        const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

        /// <summary>
        /// 火星坐标转换为百度坐标
        /// </summary>
        /// <param name="gg_lat"></param>
        /// <param name="gg_lon"></param>
        /// <returns></returns>
        public static BDcoor bd_encrypt(double gg_lat, double gg_lon)
        {
            double x = gg_lon, y = gg_lat;
            double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
            double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
            BDcoor bd = new BDcoor();
            bd.bd_lon = z * Math.Cos(theta) + 0.0065;
            bd.bd_lat = z * Math.Sin(theta) + 0.006;
            return bd;
        }

        /// <summary>
        /// 百度坐标转换为火星坐标
        /// </summary>
        /// <param name="bd_lat"></param>
        /// <param name="bd_lon"></param>
        /// <returns></returns>
        public static HDCoor bd_decrypt(double bd_lat, double bd_lon)
        {
            double x = bd_lon - 0.0065, y = bd_lat - 0.006;
            double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
            double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
            HDCoor hd = new HDCoor();
            hd.gg_lon = z * Math.Cos(theta);
            hd.gg_lat = z * Math.Sin(theta);

            return hd;
        }
    }

    public struct BDcoor
    {
        public double bd_lon { get; set; }
        public double bd_lat { get; set; }
    }

    public struct HDCoor
    {
        public double gg_lon { get; set; }
        public double gg_lat { get; set; }
    }
}
